<?
require_once("pg_mon/class_DatabaseName.php");

class WUDatabaseName extends DatabaseName {

    public function __construct($dn_id=false) {
	parent::__construct($dn_id);
    }


    public function discover() {
	if(!$this->prod_sql) {
	    $this->prod_sql=new SQL($this->get_conn_string());
	}

	$this->prod_sql->select_c("SELECT oid,nspname
	    FROM pg_namespace
	    WHERE nspname NOT IN ('pg_catalog', 'information_schema')
	    AND nspname !~ '^pg_toast'
	    AND nspname !~ '^pg_temp'");

	$web_ui_discover_condition=array('alive'=>'t');
	$this->initiate_composit('schema_name',$web_ui_discover_condition);

	$exist_ids=$this->get_composit('schema_name',COLLECTION_ID_ARRAY);
	$ret=array();

	if (!$this->prod_sql) {
	    $this->prod_sql=new SQL($this->get_conn_string);
	}

	foreach ($this->prod_sql->get_result() as $prod_sch) {
	    $exist_id=$this->composit_obj_exists('schema_name','obj_oid',$prod_sch['oid']);
	    if (!$exist_id) {
		$sn=new SchemaName;
		$sn->set_field('sch_name',$prod_sch['nspname']);
		$sn->set_field($this->composit_array['schema_name']['link_fk'],$this->id);
		$sn->set_field('obj_oid',$prod_sch['oid']);
		$sn->set_field('observable','t'); //todo: Use defined constant for config purposes 
		$sn->save();
		$this->add_composit_object('schema_name',$sn->get_id());
		$ret['added'][]=$prod_sch['nspname'];
	    } else {
		foreach ($exist_ids as $id=>$val) {
		    if ($val == $exist_id) {
			unset($exist_ids[$id]);
		    }
		}
	    }
	}
	foreach ($exist_ids as $old_id) {
	    $old_sn=new SchemaName($old_id);
	    $old_sn->set_field('alive','f');
	    $old_sn->save();
	    $this->remove_composit_object('schema_name',$old_id);

	    $ret['removed'][]=$old_sn->get_field('sch_name');
	}
	return $ret;
    }

    public function get_view() {
	$string="
<table border=0 align=center width=70%>
    <tr align=center valign=top>
        <td align=left>
	    <a href=".$_SERVER['PHP_SELF']."?level=database&action=discover&id=".$this->id.">Discover</a><br><br>
    <form method=POST action=".$_SERVER['PHP_SELF'].">
<input type=hidden name=level value=database>
<input type=hidden name=edit_dn__hc_id value=".$this->get_id().">
<input type=hidden name=id value=".$this->get_id().">
<select name=edit_dn__observable>";
	foreach (array('Observable'=>'t','Suspended'=>'f') as $name=>$val) {
	    $string.="<option value=".$val;
	    if ($val == $this->get_field('observable')) {
		$string.=" selected";
	    }
	    $string.=">".$name."</option>";
        }
	$string.="</select><br><br>
	</td><td>
	<table border=0>
	    <tr>
		<td>Database OID</td>
		<td><b>".$this->get_field('obj_oid')."</b></td>
	    </tr><tr valign=top>
		<td>Description</td>
		<td><textarea wrap name=edit_dn__description rows=7 cols=45>".($this->get_field('spec_comments') ? $this->get_field('spec_comments') : '')."</textarea></td>
	    </tr>
	</table><hr width=70%><br><input type=submit name=edit_dn__submit value=Save>&nbsp<input type=reset value=Reset></form>";
	return $string;
    }

    public function update_database($changes) {
	$old=$this->get_all_fields();
	if ($old['description'] != $changes['edit_dn__description']) {
	    $this->set_field('description',$changes['edit_dn__description']);
	}
	if ($old['observable'] != $changes['edit_dn__observable']) {
	    $this->set_field('observable',$changes['edit_dn__observable']);
	}
	$this->save();
    }

    public function __toString() {
	return $this->get_field('db_name');
    }
}

?>
